package com.tanhua.admin.service;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.tanhua.domain.Analysis;
import com.itheima.tanhua.enums.LogType;
import com.tanhua.admin.mapper.AnalysisMapper;
import com.tanhua.admin.mapper.LogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Lff
 * @date 2022/4/6
 * <p>
 * 统计日志数据
 */
@Service
public class AnalysisService {

    @Autowired
    private LogMapper logMapper;

    @Autowired
    private AnalysisMapper analysisMapper;

    /**
     * 定时统计tb_log表中的数据更新到tb_analysis表
     */
    public void analysis() {
        String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String yesterday = DateUtil.yesterday().toString("yyyy-MM-dd");
        //统计新注册数
        Integer registerNum = logMapper.queryByTypeAndLogTime(LogType.LOG_TYPE_REGISTER.getValue(), today);
        Analysis analysis = new Analysis();
        //统计活跃用户数
        Integer activeNum = logMapper.queryByLogTime(today);

        //统计次日留存数
        Integer retention1dNum = logMapper.queryNumRetention1d(today, yesterday);

        //统计登录次数
        Integer loginNum = logMapper.queryByTypeAndLogTime(LogType.LOG_TYPE_LOGIN.getValue(), today);

        analysis.setCreated(new Date());
        analysis.setNumActive(activeNum);
        analysis.setNumRegistered(registerNum);
        analysis.setNumRetention1d(retention1dNum);
        analysis.setNumLogin(loginNum);
        analysis.setRecordDate(new Date());
        //根据当前时间查询 如果存在记录就更新 不存在就添加
        LambdaQueryWrapper<Analysis> qw = Wrappers.lambdaQuery(Analysis.class);
        qw.eq(Analysis::getRecordDate, today);
        Analysis analysis1 = analysisMapper.selectOne(qw);
        if (analysis1 == null) {
            analysisMapper.insert(analysis);
        } else {
            analysis.setId(analysis1.getId());
            analysisMapper.updateById(analysis);
        }
    }

    public Analysis getAnalysisByDate(String recordDate) {
        LambdaQueryWrapper<Analysis> qw = Wrappers.lambdaQuery(Analysis.class);
        qw.eq(Analysis::getRecordDate, recordDate);
        return analysisMapper.selectOne(qw);
    }

    /**
     * 统计指定时间取键内的用户登陆数量
     *
     * @param start 开始时间
     * @param end   结束时间
     * @return
     */
    public Long countRegister(String start, String end) {
        return analysisMapper.countRegister(start, end);
    }

    /**
     * 指定时间区间内用户活跃数
     *
     * @param start
     * @param end
     * @return
     */
    public Long countActiveByDate(String start, String end) {
        return analysisMapper.countActiveByDate(start, end);
    }

    public List<Analysis> findTimeScopeAnalysis(String startDate, String endDate) {
        LambdaQueryWrapper<Analysis> qw = Wrappers.lambdaQuery(Analysis.class);
        qw.ge(Analysis::getRecordDate, startDate)
                .le(Analysis::getRecordDate, endDate)
                .orderByAsc(Analysis::getRecordDate);
        return analysisMapper.selectList(qw);
    }
    /**
     *  Map<String, Analysis> map = new HashMap<>();
     *         analyses.forEach(a -> {
     *             map.put(new SimpleDateFormat("yyyy-MM-dd").format(a.getRecordDate()), a);
     *         });
     */
}
